package com.tb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tb.mapper.IllegalityMapper;
import com.tb.model.Illegality;
import com.tb.model.IllegalityExample;
import com.tb.util.MyConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 违规处理
 * @author: wangguichun
 * @create: 2018-03-29 15:19
 **/
@Service
public class IllegalityService {

    @Autowired
    private IllegalityMapper illegalityMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QCommentService qCommentService;
    @Autowired
    private ACommentService aCommentService;

    public int getCount() {
        return illegalityMapper.countByExample(null);
    }

    public PageInfo<Illegality> getListByPageId(int pageId) {
//        int offset = 16;
//        int limit = (pageId - 1) * offset;
//        List<Illegality> list = illegalityMapper.selectListByLimit(limit, offset);
        IllegalityExample example = new IllegalityExample();
        example.setOrderByClause("time desc");
        PageHelper.startPage(pageId,MyConstant.PAGE_SIZE_ILLEGALITY);
        List<Illegality> list = illegalityMapper.selectByExample(example);

        for (Illegality illegality :
                list) {
            illegality.setFromUser(userService.getUserById(illegality.getFromId()));
            illegality.setToUser(userService.getUserById(illegality.getToId()));
            //获取违纪内容
            int type = illegality.getType();
            String content = null;
            switch (type) {
                case MyConstant.ACT_ASK:
                    content = questionService.getQuestionById(illegality.getObjectId()).getContent();
                    break;
                case MyConstant.ACT_COMMENT_QUES:
                    content = qCommentService.getQuestionCommentById(illegality.getObjectId()).getContent();
                    break;
                case MyConstant.ACT_ANSWER:
                    content = answerService.getAnswerByAnswerId(illegality.getObjectId()).getContent();
                    break;
                case MyConstant.ACT_COMMENT_ANS:
                    content = aCommentService.getAnswerCommentById(illegality.getObjectId()).getContent();
                    break;
            }
            illegality.setObjectContent(content);
        }
        return new PageInfo<>(list);
    }

    public void ignoreIllegalityById(int id) {

        Illegality illegality = illegalityMapper.selectByPrimaryKey(id);
        illegality.setStatus((byte)1);
        illegalityMapper.updateByPrimaryKeySelective(illegality);
    }

    public void deleteIllegalityById(int id) {
        Illegality illegality = illegalityMapper.selectByPrimaryKey(id);
        int type = illegality.getType();
        int objectId = illegality.getObjectId();

        //删除内容
        switch (type) {
            case MyConstant.ACT_ASK:
                questionService.deleteQuestionById(objectId);
                break;
            case MyConstant.ACT_COMMENT_QUES:
                qCommentService.deleteQuestionCommentById(objectId);
                break;
            case MyConstant.ACT_ANSWER:
                answerService.deleteAnswerByAnswerId(objectId);
                break;
            case MyConstant.ACT_COMMENT_ANS:
                aCommentService.deleteAnswerCommentById(objectId);
                break;
        }
        //删除记录
        illegalityMapper.deleteByPrimaryKey(id);

    }

    public void addRecord(Illegality illegality) {
        Integer toId = null;
        switch (illegality.getType()){
            case MyConstant.ACT_ASK:toId = questionService.getQuestionById(illegality.getObjectId()).getUserId();break;
            case MyConstant.ACT_COMMENT_QUES:toId = qCommentService.getQuestionCommentById(illegality.getObjectId()).getUserId();break;
            case MyConstant.ACT_ANSWER:toId = answerService.getAnswerByAnswerId(illegality.getObjectId()).getUserId();break;
            case MyConstant.ACT_COMMENT_ANS:toId = aCommentService.getAnswerCommentById(illegality.getObjectId()).getUserId();break;
        }
        illegality.setToId(toId);
        illegalityMapper.insertSelective(illegality);
    }

    public void removeRecordByTypeAndObjectId(byte type, int objectId) {
        IllegalityExample example = new IllegalityExample();
        IllegalityExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andObjectIdEqualTo(objectId);
        illegalityMapper.deleteByExample(example);
    }


}
